13. Classes

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

A class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).A class is a blueprint for an object. It is used as a model to define the structure of objects. An object contains data that we access through properties and that we can work on using methods. PowerShell 5.0 added the ability to create your own classes.

13.1: Listing available constructors for a class

Version ≥ 5.0

In PowerShell 5.0+ you can list available constructors by calling the static new-method without parentheses.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[DateTime]::new

OverloadDefinitions
-------------------
datetime new(long ticks)
datetime new(long ticks, System.DateTimeKind kind)
datetime new(int year, int month, int day)
datetime new(int year, int month, int day, System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second)
datetime new(int year, int month, int day, int hour, int minute, int second, System.DateTimeKind
kind)
datetime new(int year, int month, int day, int hour, int minute, int second,
System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.DateTimeKind kind)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.Globalization.Calendar calendar)
datetime new(int year, int month, int day, int hour, int minute, int second, int millisecond,
System.Globalization.Calendar calendar, System.DateTimeKind kind)

This is the same technique that you can use to list overload definitions for any method

1
> 'abc'.CompareTo
1
2
3
4
5
6
OverloadDefinitions
-------------------
int CompareTo(System.Object value)
int CompareTo(string strB)
int IComparable.CompareTo(System.Object obj)
int IComparable[string].CompareTo(string other)

For earlier versions you can create your own function to list available constructors:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function Get-Constructor {
  [CmdletBinding()]
  param(
    [Parameter(ValueFromPipeline=$true)]
    [ type ]$type
  )

  Process {
    $type.GetConstructors() |
    Format-Table -Wrap @{
      n="$($type.Name) Constructors"
      e={ ($_.GetParameters() | % { $_.ToString() }) - Join ", " }
    }
  }
}

Usage:

1
2
Get-Constructor System.DateTime
#Or [datetime] | Get-Constructor
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
DateTime Constructors
---------------------
Int64 ticks
Int64 ticks, System.DateTimeKind kind
Int32 year, Int32 month, Int32 day
Int32 year, Int32 month, Int32 day, System.Globalization.Calendar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, System.DateTimeKind
kind
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second,
System.Globalization.Calendar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond,
System.DateTimeKind kind
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond,
System.Globalization.Cal
endar calendar
Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond,
System.Globalization.Cal
endar calendar, System.DateTimeKind kind

13.2: Methods and properties

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Person {
  [string] $FirstName
  [string] $LastName
  [string] Greeting() {
    return "Greetings, {0} {1}!" -f $this.FirstName, $this.LastName
  }
}

$x = [Person]::new()
$x.FirstName = "Jane"
$x.LastName = "Doe"
$greeting = $x.Greeting() # "Greetings, Jane Doe!"

13.3: Constructor overloading

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Person {
  [string] $Name
  [int] $Age
}

Person([string] $Name) {
  $this.Name = $Name
}

Person([string] $Name, [int]$Age) {
  $this.Name = $Name
  $this.Age = $Age
}

13.4: Get All Members of an Instance

1
 Get-Member -InputObject $anObjectInstance

This will return all members of the type instance. Here is a part of a sample output for String instance

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
TypeName: System.String

Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string
strB), i...
Contains Method bool Contains(string value)
CopyTo Method void CopyTo(int sourceIndex, char[] destination, int
destinationI...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value,
System.S...
Equals Method bool Equals(System.Object obj), bool Equals(string value),
bool E...
GetEnumerator Method System.CharEnumerator GetEnumerator(),
System.Collections.Generic...
GetHashCode Method int GetHashCode()
GetType Method type GetType()

13.5: Basic Class Template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Define a class
class TypeName
{
  # Property with validate set
  [ValidateSet("val1", "Val2")]
  [string] $P1

  # Static property
  static [hashtable] $P2

  # Hidden property does not show as result of Get-Member
  hidden [int] $P3

  # Constructor
  TypeName ([string] $s)
  {
    $this.P1 = $s
  }

  # Static method
  static [void] MemberMethod1([hashtable] $h)
  {
    [TypeName]::P2 = $h
  }

  # Instance method
  [int] MemberMethod2([int] $i)
  {
    $this.P3 = $i
    return $this.P3
  }
}

13.6: Inheritance from Parent Class to Child Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class ParentClass
{
  [string] $Message = "It's under the Parent Class"
  [string] GetMessage()
  {
    return ("Message: {0}" -f $this.Message)
  }
}

# Bar extends Foo and inherits its members
class ChildClass : ParentClass
{
}
$Inherit = [ChildClass]::new()

SO, $Inherit.Message will give you the

1
"It's under the Parent Class"